﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:s="clr-namespace:DragResizeRotateItems"
                     
                    xmlns:l="clr-namespace:SharpMap.Wpf">

  <!-- RotateThumb Style -->
  <Style TargetType="{x:Type s:RotateThumb}">
    <Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
    <Setter Property="Cursor" Value="Hand"/>
    <Setter Property="Control.Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type s:RotateThumb}">
          <Grid Width="30" Height="30">
            <Path Fill="#AAD0D0DD" Stretch="Fill" Data="M 50,100 A 50,50 0 1 1 100,50 H 50 V 100"/>
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <!-- RotateDecorator Template -->
  <ControlTemplate x:Key="RotateDecoratorTemplate" TargetType="{x:Type Control}">
    <Grid>
      <s:RotateThumb Margin="-18,-18,0,0" VerticalAlignment="Top" HorizontalAlignment="Left"/>
      <s:RotateThumb Margin="0,-18,-18,0" VerticalAlignment="Top" HorizontalAlignment="Right">
        <s:RotateThumb.RenderTransform>
          <RotateTransform Angle="90" />
        </s:RotateThumb.RenderTransform>
      </s:RotateThumb>
      <s:RotateThumb Margin="0,0,-18,-18" VerticalAlignment="Bottom" HorizontalAlignment="Right">
        <s:RotateThumb.RenderTransform>
          <RotateTransform Angle="180" />
        </s:RotateThumb.RenderTransform>
      </s:RotateThumb>
      <s:RotateThumb Margin="-18,0,0,-18" VerticalAlignment="Bottom" HorizontalAlignment="Left">
        <s:RotateThumb.RenderTransform>
          <RotateTransform Angle="270" />
        </s:RotateThumb.RenderTransform>
      </s:RotateThumb>
    </Grid>
  </ControlTemplate>

  <!-- ResizeDecorator Template -->
  <ControlTemplate x:Key="ResizeDecoratorTemplate" TargetType="{x:Type Control}">
    <Grid SnapsToDevicePixels="True" Opacity="1">
      <s:ResizeThumb Height="3" Cursor="SizeNS" Margin="0 -4 0 0" SnapsToDevicePixels="True"
                     VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
      <s:ResizeThumb Width="3" Cursor="SizeWE" Margin="-4 0 0 0" SnapsToDevicePixels="True"
                     VerticalAlignment="Stretch" HorizontalAlignment="Left"/>
      <s:ResizeThumb Width="3" Cursor="SizeWE" Margin="0 0 -4 0" SnapsToDevicePixels="True"
                     VerticalAlignment="Stretch" HorizontalAlignment="Right"/>
      <s:ResizeThumb Height="3" Cursor="SizeNS" Margin="0 0 0 -4" SnapsToDevicePixels="True"
                     VerticalAlignment="Bottom" HorizontalAlignment="Stretch"/>
      <s:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="-6 -6 0 0" SnapsToDevicePixels="True"
                     VerticalAlignment="Top" HorizontalAlignment="Left"/>
      <s:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="0 -6 -6 0" SnapsToDevicePixels="True"
                     VerticalAlignment="Top" HorizontalAlignment="Right"/>
      <s:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="-6 0 0 -6" SnapsToDevicePixels="True"
                     VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
      <s:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="0 0 -6 -6" SnapsToDevicePixels="True"
                     VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
    </Grid>
  </ControlTemplate>

  <!-- DragThumb Template -->
  <ControlTemplate x:Key="DragThumbTemplate" TargetType="{x:Type s:DragThumb}">
    <Rectangle Fill="Transparent"/>
  </ControlTemplate>

  
  <!-- ContentControl template to rotate, drag and resize items -->
  <Style TargetType="ContentControl" x:Key="sssstyle">
    <Setter Property="MinHeight" Value="10"/>
    <Setter Property="MinWidth" Value="10"/>
    <Setter Property="SnapsToDevicePixels" Value="true"/>
    <Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
    <Setter Property="Tag" Value="IsSelected"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="l:ContentLayerItem">
          <Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
            <Control Name="PART_RotateDecorator" Template="{StaticResource RotateDecoratorTemplate}" Visibility="Collapsed"/>
            <s:DragThumb Template="{StaticResource DragThumbTemplate}" Cursor="SizeAll"/>
            <Control x:Name="PART_ResizeDecorator" Template="{StaticResource ResizeDecoratorTemplate}" Visibility="Collapsed"/>
            <ContentPresenter Content="{TemplateBinding ContentControl.Content}"
                             l:GeoPanel.WorldX="{Binding RelativeSource={RelativeSource Self}, Path=Content.(prim:ContentLayerPanel.WorldX), Mode=TwoWay}"
                              l:GeoPanel.WorldY="{Binding RelativeSource={RelativeSource Self}, Path=Content.(prim:ContentLayerPanel.WorldY), Mode=TwoWay}"/>
          </Grid>
          <!-- this trigger is for demo purposes only - to simulate selection of items-->
          <ControlTemplate.Triggers>
            <DataTrigger Value="IsSelected" Binding="{Binding RelativeSource={RelativeSource Self},Path=Tag}">
              <Setter TargetName="PART_ResizeDecorator" Property="Visibility" Value="Visible"/>
              <Setter TargetName="PART_RotateDecorator" Property="Visibility" Value="Visible"/>
            </DataTrigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
  
</ResourceDictionary>